﻿using System;

namespace Lesson10_练习题
{
    #region 练习一
    //定义一个位置结构体或类，为其重载一个运算符判断是否相等
    struct Vector
    {
        public float x;
        public float y;
        public Vector(float x, float y)
        {
            this.x = x;
            this.y = y;
        }
        public static bool operator ==(Vector v1, Vector v2)
        {
            if (v1.x == v2.x && v1.y == v2.y)
            {
                return true;
            }
            return false;
        }
        public static bool operator !=(Vector v1, Vector v2)
        {
            if(v1.x != v2.x || v1.y != v2.y)
            {
                return true;
            }
            return false;
        }
    }
    #endregion

    #region 练习二
    //定义一个Vector3类(x,y,z),通过重载运算符实现如下操作
    //向量加减
    //向量数乘
    class Vector3
    {
        public float x;
        public float y;
        public float z;
        public Vector3(float x, float y, float z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }
        public Vector3() : this(0 ,0 ,0)
        {
            //使用this关键字调用另一个构造函数以节省代码量
        }
        public static Vector3 operator +(Vector3 v1, Vector3 v2)
        {
            //直接使用对象初始化器以简化代码
            //可以与下面几个运算符重载对比
            return new Vector3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
        }
        public static Vector3 operator -(Vector3 v1, Vector3 v2)
        {
            Vector3 v = new Vector3();
            v.x = v1.x - v2.x;
            v.y = v1.y - v2.y;
            v.z = v1.z - v2.z;
            return v;
        }
        public static Vector3 operator *(Vector3 v1, float value)
        {
            Vector3 v = new Vector3();
            v.x = v1.x * value;
            v.y = v1.y * value;
            v.z = v1.z * value;
            return v;
        }
    }
    static class Tools
    {
        public static void PrintInfo(Vector3 v)
        {
            Console.WriteLine("{0},{1},{2}", v.x, v.y, v.z);
        }
    }
    #endregion
    class Program
    {
        static void Main(string[] args)
        {
            #region 练习一
            Vector v1 = new Vector(1,1);
            Vector v2 = new Vector(1,2);
            if(v1 ==  v2)
            {
                Console.WriteLine("相等");
            }
            #endregion

            #region 练习二
            Vector3 vector1 = new Vector3(1,1,1);
            Vector3 vector2 = new Vector3(1,1,1);
            int value = 10086;
            Tools.PrintInfo(vector1 - vector2);
            Tools.PrintInfo(vector2 - vector1);
            Tools.PrintInfo(vector1 + vector2);
            Tools.PrintInfo(vector1 * value);
            Tools.PrintInfo(vector2 * value);
            #endregion
        }
    }
}